import Information from "../../model/information"
import { debounce, getDataSet, getEventParam } from "../../utils/util";
const information = new Information()
Page({
  data: {
    params: {}, //拒绝参会参数
    show: false, //拒绝参会
    tabs: [{name:'评审邀请', key:3},{name: '项目审核',key: 4}],
    tabIndex: 3, // 当前tab下标
    list: [], // 列表
    value: '', // 拒绝原因
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._getList()
  },
  onShow() {
    this._getList()
  },
  handleTabChange: debounce(async function (e) {
    this.data.tabIndex = e.detail.index
    await this._getList()
  }),
  handleScrollToTop: function () {
    wx.pageScrollTo({
        scrollTop: 0
    })
  },
  // 获取列表 API
  async _getList() {
    const list = await    information.reset().getMyMessage(this.data.tabIndex)
    this.setData({
      list
    })
  },
  // 同意参会
  async handleSure(e) {
    const {id, projectId,  mailType} = getDataSet(e, 'meeting')
    let query = {
      mailId: id,
      projectId,
      isParticipants: "1",
      mailType
    }
    await wx.showModal({
      title: '注意',
      content: '是否同意参会',
      complete: async (res) => {
        if (res.confirm) {
          let result = await Information.updateExpertsParticipants(query)
          if (result.code == 200) {
            wx.showToast({
              title: '确认参会',
              duration: 2000
            })
            this._getList()
          } else {
            wx.showModal({
              content: result.msg
            })
          }
        }
      }
    })
  },
  // 拒绝参会
  async handleCancel(e) {
    let { id, senderId, mailSendId, projectId, mailType} = getDataSet(e, 'meeting')
    let query = {
      isParticipants: '0',
      content: '',
      receivers: senderId,
      typeSourceId: mailSendId,
      mailId: id,
      projectId,
      mailType
    }
    this.setData({show: true, value: '', params: query})
  },
  // 确认拒绝参会
  async handleUpdata() {
    this.data.params.content = this.data.value
    let res = await Information.updateExpertsParticipants(this.data.params)
    if(res.code == 200) {
      wx.showToast({
        title: '修改成功',
        duration: 2000
      })
      this._getList()
      this.setData({show: false})
    } else {
      this.setData({show: false})
      wx.showModal({
        content: res.msg
      })
    }
  },
  // 处理催办
  async handleUrge(e) {
    let that = this
    const urge = getDataSet(e, 'urge')
    // 1. 修改消息状态
    let res = await Information.updateCheckStatus({
      mailId: urge.id,
      projectId: urge.projectId
    })
    if(res.code === 200) {
      let project = res.data
      // 2. 跳转项目详情列表
      wx.navigateTo({
        url: `/pages/project-detail/project-detail?project_id=${project.projectId}&manager_id=${project.managerId}&dept_type=${project.deptType}&tempParent_id=${project.projectClassification}`,
        events: {
          onResetList: function () {
            wx.showToast({
              title: '已催办',
              duration: 2000
            })
            that._getList()
          }
        }
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  async onPullDownRefresh() {
    // 简写
    this._getList()
    // 主动回弹
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    // 获取下一页的数据
    if(!information.hasMoreData) {
      // 如果是最后一页,不走接口调用
      return
    }
    const list = await information.getMyMessage(this.data.tabIndex)
    this.setData({
      list: list
    })
  },
  onShow() {
    this._getList()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})